﻿using SIMS.SqlSugarWebApi.ServiceInterface;

namespace SIMS.SqlSugarWebApi.Services
{
    public class BaseService<T> : IBaseService<T> where T : class, new()
    {
        private readonly DataContext _context;

        public BaseService(DataContext context)
        {
            _context = context;
        }

        public async Task<int> AddEntity(T t)
        {
            return await _context.db.Insertable(t).ExecuteCommandAsync();
        }

        public async Task<int> DeleteEntity(int id)
        {
            return await _context.db.Deleteable<T>().In(id).ExecuteCommandAsync();
        }

        public async Task<T> GetEntity(int id)
        {
            return await _context.db.Queryable<T>().InSingleAsync(id);
        }

        public async Task<int> UpdateEntity(T t)
        {
            return await _context.db.Updateable(t).ExecuteCommandAsync();
        }
    }
}
